package com.xjm.ips.service;

import com.xjm.ips.entity.Menu;
import com.xjm.ips.entity.Role;
import com.xjm.ips.mapper.MenuRoleMapper;
import com.xjm.ips.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleService {
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    MenuRoleMapper menuRoleMapper;

    public List<Role> getAllRoles() {
        return roleMapper.getAllRoles();
    }

    public int addRole(Role role) {
        if(!role.getName().startsWith("ROLE_")){
            role.setName("ROLE_"+role.getName());
        }
        return roleMapper.insert(role);
    }

    @Transactional
    public Integer deleteRoleById(Integer rid) {
        menuRoleMapper.deleteByRoleId(rid);
        return roleMapper.deleteByPrimaryKey(rid);
    }
}
